const Koa = require('koa');
const mount = require('mount-koa-routes');
const bodyParser = require('koa-bodyparser');
const middleware = require('./middleware/index');

const app = new Koa();

app.use(bodyParser());

// 设置跨域和相应数据格式
app.use(middleware.cors);

// 统一响应机制
app.use(middleware.resextra);

// 接口token验证
app.use(middleware.auth);

// 统一自动挂载路由
mount(app, __dirname + '/routes', true, '/api/private/v1');

// 统一处理无响应,如果没有路径处理就返回 Not Found
app.use(async (ctx, next) => {
  ctx.sendResult(null, 404, 'Not Found ');
});

// 端口监听
const port = 3000;
app.listen(port, () => {
  console.log(`server is running: http://localhost:${port}`);
});
